package net.thermetics.fx.services.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import net.thermetics.fx.services.DynamicResourceService;

public class DynamicResourceServiceImpl implements DynamicResourceService {

	@Override
	public String getScript(String widget, String name) {
		String path = "net/thermetics/fx/widgets/" + widget + "/" + name + ".js";
		InputStream stream = this.getClass().getClassLoader().getResourceAsStream(path);
		
		if (stream != null) {
			String code = "";
			String line = null;
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
			
			try {
				while ((line = reader.readLine()) != null) {
					code += line + "\n";
				}
			}
			catch (IOException e) {
				e.printStackTrace();
				return null;
			}
			
			return code;
		}
		else {
			return null;
		}
	}

	@Override
	public String getStylesheet(String widget, String sheetName) {
		String path = "net/thermetics/fx/widgets/" + widget + "/" + sheetName + ".css";
		InputStream stream = this.getClass().getClassLoader().getResourceAsStream(path);
		
		if (stream != null) {
			String code = "";
			String line = null;
			
			BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
			
			try {
				while ((line = reader.readLine()) != null) {
					code += line + "\n";
				}
			}
			catch (IOException e) {
				e.printStackTrace();
				return null;
			}
			
			return code;
		}
		else {
			return null;
		}
	}

}
